from datetime import datetime, timedelta
import calendar


def get_time_between(start_time, end_time, type=None):
    # 将时间字符串转换为 datetime 对象
    # 初始化结果字典
    result = {}
    # 计算时间差
    time_diff = end_time - start_time
    # 如果时间差大于等于30天，则计算小时数
    if type == 'hours':
        for i in range(0, time_diff.days * 24 + time_diff.seconds // 3600 + 1):
            hour = start_time + timedelta(hours=i)
            result[hour.strftime("%Y-%m-%d %H:%M:%S")] = 0.0
    else:
        for i in range(0, time_diff.days * 24 * 60 + time_diff.seconds // 60 + 1):
            minute = start_time + timedelta(minutes=i)
            result[minute.strftime("%Y-%m-%d %H:%M:%S")] = 0.0
    return result


def last_month_time_intervals():
    """
    获取上一个自然月的时间列表，指定时间间隔，并返回上一个自然月的年份和月份

    参数:
    interval_minutes (int): 时间间隔（分钟）

    返回值:
    tuple: (年份, 月份, 时间列表)
    """
    # 获取当前日期
    today = datetime.today()
    # 获取上一个月的年份和月份
    last_month = today.replace(day=1) - timedelta(days=1)
    last_month_year = last_month.year
    last_month_month = last_month.month

    # 上一个月的开始日期和结束日期
    start_date = datetime(last_month_year, last_month_month, 1, 0, 0)
    end_date = datetime(last_month_year, last_month_month, calendar.monthrange(last_month_year, last_month_month)[1],
                        23, 55)

    # 时间间隔
    interval = timedelta(minutes=5)

    # 生成时间列表
    time_list = [start_date]
    current_time = start_date
    while current_time < end_date:
        current_time += interval
        time_list.append(current_time)

    return (last_month_year, last_month_month, time_list)
